package com.gxa.teax.admin.controller;


import com.gxa.teax.cores.utils.JsonResult;
import com.gxa.teax.cores.utils.PasswordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Pattern;
import java.util.Map;

/**
 * 管理控制器
 * @author wangpeng
 * @date 2022/07/19
 */
@Api(tags = "管理员接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @PostMapping("login")
    @ApiOperation("登录接口")
    public Map<String, Object> login(@RequestParam(value = "11位手机号") @Pattern(regexp = "^((13[0-9])|(15[^4,\\D])|(14[57])|(17[0])|(17[7])|(18[0,0-9]))\\d{8}$") String phone
            , @RequestParam(value = "必须包含字母数字的6-16位密码") @Pattern(regexp = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$") String pwd) {
        Subject subject = SecurityUtils.getSubject();
        JsonResult jsonResult = new JsonResult();
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(phone, PasswordUtils.getMd5Utils(pwd));
            subject.login(token);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
        } else {
            jsonResult.setMsg("无需再次登录");
        }
        return jsonResult.getMap();
    }

}

